var localhost = '172.16.5.236'

$.get('http://'+localhost+':8000/web/poiQuery/', function (data) {
    var idlist = data.poi_list;
    var optgroup;
    var option;
    for (i = 0; i < idlist.length; i++) {
        optgroup = document.getElementById("optgrouppoi");
        option = document.createElement("option");
        option.innerHTML = idlist[i]; //添加内容
        optgroup.appendChild(option);
    }
});



function ShowUniquePOI() {
    name_poi = jQuery("#selectpoi  option:selected").text();
    if (name_poi == '--所有--') {
        POILayer = new ol.layer.Vector({
            source: new ol.source.Vector({
                url: 'http://'+localhost+':8080/geoserver/taxidm/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=wuhan%3Awhpoi&maxFeatures=50000&outputFormat=application%2Fjson',
                format: new ol.format.GeoJSON()
            }),
            style: function (feature, resolution) {
                var id = feature.get('fclass');
                var style = null;
                style = new ol.style.Style({
                    image: new ol.style.Icon({
                        src: '../static/img/标记.svg', //图标路径
                        anchor: [0.5, 1], //锚点
                        scale: 0.1, //大小
                        rotation: 0 //旋转角度
                    })
                });
                return [style]
            }
        });
        map.addLayer(POILayer);
    } else {
        var featureRequest = new ol.format.WFS().writeGetFeature({
            srsName: 'EPSG:4326',
            featureNS: 'http://localhost:8080/taxidm',
            featurePrefix: 'taxidm',
            featureTypes: ['whpoi'],
            outputFormat: 'application/json',
            filter: ol.format.filter.equalTo('fclass', name_poi)
        });

        fetch('http://'+localhost+':8080/geoserver/wfs', {
            method: 'POST',
            body: new XMLSerializer().serializeToString(featureRequest)
        }).then(function (response) {
            return response.json();
        }).then(function (json) {
            var features = (new ol.format.GeoJSON({
                featureProjection: 'EPSG:3857'
            })).readFeatures(json)
            if (features.length == 0) {
                alert('没有数据')
            } else {
                var source = new ol.source.Vector({
                    features: features
                });
                POILayer = new ol.layer.Vector({
                    source: source,
                    style: function (feature, resolution) {
                        var id = feature.get("fclass");
                        var style = null;
                        if (id == 'hospital') {
                            style = new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/img/医院.svg', //图标路径
                                    anchor: [0.5, 1], //锚点
                                    scale: 0.2, //大小
                                    rotation: 0 //旋转角度
                                })
                            });
                        } else if (id == 'town_hall') {
                            style = new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/img/政务大厅.svg', //图标路径
                                    anchor: [0.5, 1], //锚点
                                    scale: 0.1, //大小
                                    rotation: 0 //旋转角度
                                })
                            });
                        } else if (id == 'school' || id == 'kindergarten') {
                            style = new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/img/学校.svg', //图标路径
                                    anchor: [0.5, 1], //锚点
                                    scale: 0.2, //大小
                                    rotation: 0 //旋转角度
                                })
                            });
                        } else if (id == 'bookshop' || id == 'library') {
                            style = new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/img/书店.svg', //图标路径
                                    anchor: [0.5, 1], //锚点
                                    scale: 0.2, //大小
                                    rotation: 0 //旋转角度
                                })
                            });
                        } else if (id == 'police') {
                            style = new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/img/警察局.svg', //图标路径
                                    anchor: [0.5, 1], //锚点
                                    scale: 0.2, //大小
                                    rotation: 0 //旋转角度
                                })
                            });
                        } else if (id == 'restaurant') {
                            style = new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/img/饭店.svg', //图标路径
                                    anchor: [0.5, 1], //锚点
                                    scale: 0.2, //大小
                                    rotation: 0 //旋转角度
                                })
                            });
                        } else if (id == 'college' || id == 'university') {
                            style = new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/img/大学.svg', //图标路径
                                    anchor: [0.5, 1], //锚点
                                    scale: 0.2, //大小
                                    rotation: 0 //旋转角度
                                })
                            });
                        } else if (id == 'bank') {
                            style = new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/img/银行.svg', //图标路径
                                    anchor: [0.5, 1], //锚点
                                    scale: 0.2, //大小
                                    rotation: 0 //旋转角度
                                })
                            });
                        } else if (id == 'attraction') {
                            style = new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/img/旅游景点.svg', //图标路径
                                    anchor: [0.5, 1], //锚点
                                    scale: 0.15, //大小
                                    rotation: 0 //旋转角度
                                })
                            });
                        } else if (id == 'cafe') {
                            style = new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/img/咖啡.svg', //图标路径
                                    anchor: [0.5, 1], //锚点
                                    scale: 0.2, //大小
                                    rotation: 0 //旋转角度
                                })
                            });
                        } else {
                            style = new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/img/标记.svg', //图标路径
                                    anchor: [0.5, 1], //锚点
                                    scale: 0.21, //大小
                                    rotation: 0 //旋转角度
                                })
                            });
                        }
                        return [style]
                    }
                });
                map.addLayer(POILayer);
            }
        });
    }
}

function ClearPOI() {
    map.removeLayer(POILayer);
}



var coordinate1;
var coordinate2;

function FirstXY() {
    key1=map.on('click', function (e) {
        coordinate1 = e.coordinate;
        coordinate1 = ol.proj.transform(e.coordinate, 'EPSG:3857', 'EPSG:4326')
        coordinate1[0] = coordinate1[0].toFixed(2);
        coordinate1[1] = coordinate1[1].toFixed(2);
        document.getElementById("xyfirst").innerHTML = "first：经度：" + coordinate1[0] + "纬度：" + coordinate1[1];
    }.bind(this));
}    

function SecondXY() {
    key2=map.on('click', function (e) {
        coordinate2 = e.coordinate;
        coordinate2 = ol.proj.transform(e.coordinate, 'EPSG:3857', 'EPSG:4326')
        coordinate2[0] = coordinate2[0].toFixed(2);
        coordinate2[1] = coordinate2[1].toFixed(2);
        document.getElementById("xysecond").innerHTML = "first：经度：" + coordinate2[0] + "纬度：" + coordinate2[1];
    }.bind(this));
}

function ClearBind(){
    ol.Observable.unByKey(key1);
}

function RouteQuery() {
    // var waypoints = [
    //     [coordinate1[0], coordinate2[1]],
    //     [coordinate2[0], coordinate2[1]]
    // ]
    var waypoints = [
        [114.3, 30.5],
        [114.263, 30.48]
    ]
    console.log(waypoints);
    var control = new olrm.Control({
        map,
        waypoints
    });
    map.addControl(control)
}

